<template>
  <div class="table-bar">
    <div 
      class="tab-item" 
      v-for="(tab, index) of props.dataList" 
      :key="index"
      @click="handleClicktabItem(index)"
    >
      <router-link :to="tab.path">
        <div class="item-wrap">
          <div :class="['iconfont', tab.icon ]"  :style="{ color: handleIsSelect(index, tab) }"></div>
          <div class="tab-name" :style="{ color: handleIsSelect(index, tab) }">{{ tab.name }}</div>
        </div>
      </router-link>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';

const props = defineProps({
  initIndex: { type: Number, default: 0 },
  dataList: { type: Array, default: () => ([]) }
})

// 保持一致性
const curIndex = ref( props.initIndex );

const handleClicktabItem = (index) => {
  curIndex.value = index;
}

//  判断是否被选中, 并给定颜色
const handleIsSelect = (index, data) => {
  if(curIndex.value === index) {
    return data.selectColor;
  } 

  return data.color;
}


</script>

<style lang="scss" scoped>
.table-bar {
  display: flex;
  width: 100vw;
  height: 6rem;
  background: #fff;
  border-top: .01rem solid #ccc;

  .tab-item {
    flex: 1;
    box-sizing: border-box;

    .iconfont {
      font-size: 2rem;
    }

    .item-wrap {
      display: flex;
      height: 100%;
      flex-direction: column;
      justify-content: center;
      align-items: center;

      .tab-name {
        font-size: 1.2rem;
        padding-top: .4rem;
      }
    }
  }
}
</style>